package com.lvwenguo.service.serviceImple;

import com.lvwenguo.dao.BootmDao;
import com.lvwenguo.dao.EchartsCategoryDao;
import com.lvwenguo.model.EchartsCategory;
import com.lvwenguo.utils.JdbcUtil;
import com.lvwenguo.utils.SortByIndex;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class EchartsServiceImple {
    private Connection connection;
    private JdbcUtil jdbcUtil;
    private EchartsCategoryDao categoryDao;
    public EchartsServiceImple(){
        connection=JdbcUtil.getJdbcUtil().getConnertion();
        categoryDao=new EchartsCategoryDao(connection);
    }
    public  List<EchartsCategory> TuBiao(){
        String sql="select c.category_name,s.sale_count from biz_sale s LEFT JOIN biz_product p on s.product_bar_code=p.bar_code LEFT JOIN biz_category c on p.category_id=c.id ORDER BY s.sale_count DESC";
    String sqlCategory="select c.category_name from biz_category c ";
        Integer sum=0;
        List<EchartsCategory> list=new ArrayList<EchartsCategory>();


    try {
        //查询数据库
            List<EchartsCategory> echartsCategories = categoryDao.selectList(sql, EchartsCategory.class);
            List<EchartsCategory> echartsCategories1 = categoryDao.selectList(sqlCategory, EchartsCategory.class);
    //使用循环，对比相同字段销售数量进行相加
            for(EchartsCategory echartsCategory1:echartsCategories1){
            for(EchartsCategory echartsCategory:echartsCategories){
                //判断品类名是否相同
                    if(echartsCategory1.getCategory_name().equalsIgnoreCase(echartsCategory.getCategory_name())){
                        //进行相加
                        sum=echartsCategory.getSale_count()+sum;
                    }
                }
            //添加集合
            list.add(new EchartsCategory(echartsCategory1.getCategory_name(),sum));
            //sum归0
            sum=0;

            }
            //进行排序，根据字段
            Collections.sort(list,new SortByIndex());

        return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
return list;

    }

}
